import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/home.dart';
import 'package:flutter_house_manager/pages/Home/HomeView/home_list.dart';
import 'package:flutter_house_manager/pages/Home/HomeView/home_nav.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';
import 'package:flutter_house_manager/utils/toast.dart';

// ignore: must_be_immutable
class HomeView extends StatefulWidget {
  const HomeView({super.key});

  @override
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  // 公告列表
  List list = [];
  @override
  void initState() {
    super.initState();
    // 获取数据
    getAnnounceList();
  }

  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    try {
      // 请求数据
      var res = await getAnnounceListAPI();
      PromptAction.showSuccess('获取数据成功');
      // 处理数据
      setState(() {
        //渲染数据
        list = res;
      });
    } catch (e) {
      PromptAction.showError('获取数据失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "享+社区",
          style: TextStyle(fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
      ),
      body: ListView(
          // 子组件分为三部分
          children: [
            // 按钮组件
            ...getBtnChild(),
            // 导航组件
            const HomeNav(),
            Image.asset('assets/images/banner@2x.jpg'),
            // 列表组件
            HomeList(list: list),
          ]),
    );
  }
}

List<Widget> getBtnChild() {
  return [
    ElevatedButton(
        onPressed: () {
          tokenManager.setToken('123456wq');
        },
        child: const Text("设置token")),
    ElevatedButton(
        onPressed: () async {
          var token = tokenManager.getToken();
          PromptAction.showSuccess(token);
        },
        child: const Text("获取token")),
    ElevatedButton(
        onPressed: () {
          tokenManager.removeToken();
        },
        child: const Text("删除token"))
  ];
}

int _count = 60;
Timer? _timer;

